package com.xmkj.bearuser.jifen;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.widget.TextView;

import com.common.Entity.JiSuanEntity;
import com.common.cons.Constans;
import com.common.mvp.BaseMvpActivity;
import com.common.mvp.BasePresenter;
import com.common.retrofit.method.JisuanDetailMethods;
import com.common.retrofit.subscriber.CommonSubscriber;
import com.common.retrofit.subscriber.SubscriberListener;
import com.common.utils.EmptyUtils;
import com.common.utils.SPUtils;
import com.common.widget.recyclerview.refresh.recycleview.XRecyclerView;
import com.xmkj.bearuser.R;
import com.xmkj.bearuser.TempItemBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Create by leo
 * 创建日期：2018/5/23
 * 描述：
 */

public class JiSuanActivity extends BaseMvpActivity {
    public static final String TEMPBEAN = "tempbena";
    @BindView(R.id.recyclerview)
    XRecyclerView recyclerview;
    @BindView(R.id.tv_ssc_code)
    TextView tvSscCode;
    @BindView(R.id.tv_absolute_min)
    TextView tvAbsoluteMin;
    @BindView(R.id.tv_lucky_num)
    TextView tvLuckyNum;
    private TempItemBean tempItemBean;
    List<String> bean = new ArrayList<>();
    private JisuanAdapter adapter;

    @Override
    protected BasePresenter createPresenterInstance() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_jiuandetail;
    }

    @Override
    protected void onViewCreated() {

    }

    @Override
    protected void doLogicFunc() {
        tempItemBean = (TempItemBean) getIntent().getSerializableExtra(TEMPBEAN);
        if (EmptyUtils.isNotEmpty(tempItemBean)) {
            gotoHttpRep();
            setRecyclerView();
        }
    }

    @Override
    protected void setNavigation() {
        setNavigationBack("计算详情");
    }

    private void setRecyclerView() {
        mPageSize = 200000 ;
//        IRecyclerViewHelper.init().setRecycleLineLayout(context, LinearLayoutManager.VERTICAL, recyclerview);
        GridLayoutManager layoutManager = new GridLayoutManager(context, 4);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerview.setLayoutManager(layoutManager);
//        recyclerview.addItemDecoration(new DividerGridItemDecoration(context));
      /*  recyclerview.addItemDecoration(new MarginDecoration(LinearLayoutManager.VERTICAL, (int) SizeUtils.dp2px(context, 1)));
        recyclerview.addItemDecoration(new MarginDecoration(LinearLayoutManager.HORIZONTAL, (int) SizeUtils.dp2px(context, 1)));*/
//        recyclerview.addItemDecoration(new MarginDecoration(LinearLayoutManager.VERTICAL, (int) SizeUtils.dp2px(context, 1)));
        recyclerview.setHasFixedSize(true);
        recyclerview.setRefreshing(true);
        recyclerview.setRefreshEnabled(false);
        recyclerview.setLoadMoreEnabled(true);
        adapter = new JisuanAdapter(context, bean);
//        adapter.setOnItemClickSeclect(this);
        recyclerview.setAdapter(adapter);
        recyclerview.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                mPageIndex = 1;
                mIsRefreshOrLoadMore = 0;
                gotoHttpRep();
            }

            @Override
            public void onLoadMore() {
                if (mIsHasNoData) {
                    recyclerview.loadMoreComplete();
                    recyclerview.setNoMore(true);
                    return;
                }
                mPageIndex++;
                mIsRefreshOrLoadMore = 1;
                gotoHttpRep();
            }
        });
    }


    private void gotoHttpRep() {
        CommonSubscriber<JiSuanEntity> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                dismissProgressDialog();
                JiSuanEntity beans = (JiSuanEntity) o;
                tvSscCode.setText("最新一期揭晓结果：" + beans.ssc_code + "第（" + beans.ssc_vol + ") 期");
                tvAbsoluteMin.setText("差值最小绝对值: " + beans.absolute_min);
                tvLuckyNum.setText("幸运号码：" + beans.lucky_num);
                List<String> beanList = beans.all_code;
                recyclerview.loadMoreComplete();
                // 下拉刷新
                if (mIsRefreshOrLoadMore == 0) {
                    recyclerview.refreshComplete();
                    adapter.clear();
                }

                if (EmptyUtils.isNotEmpty(beanList)) {
                    bean = beanList;
                    adapter.addAll(bean);
                } else if (mIsRefreshOrLoadMore == 0) {
//                    statusEmpty();
                    return;
                }

                if (EmptyUtils.isEmpty(beanList)) {
                    recyclerview.setNoMore(true);
                } else {
                    mIsHasNoData = beanList.size() < mPageSize;
                    recyclerview.setNoMore(mIsHasNoData);
                }
                adapter.notifyDataSetChanged();
                recyclerview.refreshComplete();
                recyclerview.loadMoreComplete();
                if (mIsRefreshOrLoadMore == 0) {
                    dismissProgressDialog();
                }
            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                adapter.notifyDataSetChanged();
                recyclerview.refreshComplete();
                recyclerview.loadMoreComplete();
                if (mIsRefreshOrLoadMore == 0) {
                    dismissProgressDialog();
                }
            }
        });


        JisuanDetailMethods.getInstance().duobaoCalculate(subscriber, SPUtils.getShareInt(Constans.UID), SPUtils.getShareString(Constans.HASHID), tempItemBean.gid, tempItemBean.vid + "", tempItemBean.fvid);
        rxManager.add(subscriber);
    }

    @Override
    public void bindKnife() {
        ButterKnife.bind(this);

    }
}
